package org.bulu.project.web.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bulu.project.common.result.Result;
import org.bulu.project.system.model.entity.Dynamic;
import org.bulu.project.system.model.entity.Favorite;
import org.bulu.project.system.model.entity.UserVideo;
import org.bulu.project.system.model.entity.Video;
import org.bulu.project.system.security.SecurityUtils;
import org.bulu.project.system.service.*;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


//个人空间相关操作
@RestController
@RequestMapping("/api/v1/palspcenter")
@RequiredArgsConstructor
@Slf4j
public class PerSpController {

    private final VideoService videoService ;
    private final FavoriteService favoriteService;
    private final FavoriteVideoService favoriteVideoService;
    private final UserVideoService userVideoService;
    private final DynamicService dynamicService;

    //我的视频框
    //获取用户上传的视频列表
    @GetMapping("/findAllMY")
    public Result findAllMY() {
        Long uid = SecurityUtils.getUserId();
        return Result.success(videoService.getVideosByIds(uid));
    }
    //选择某个视频删除
    @GetMapping("/delete")
    public Result delete(Integer vid) {
        try {
            return Result.success(videoService.delVideo(vid));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //修改视频信息
    @PostMapping("/updatesp")
    public Result update(@RequestBody  Video video) {
        try {
            return Result.success(videoService.updateVideo(video));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //我的收藏夹框
    //获取用户收藏了的收藏夹列表
    @GetMapping("/collected-fids")
    public Result  getCollectedFids(Integer uid) {
        List<Integer> fids = favoriteService.getFavorites(uid);
        List<Favorite> favorites = new ArrayList<>();
        for (Integer fid : fids) {
            favorites.add(favoriteService.getFavoriteById(fid));
        }
        return Result.success(favorites);
    }
    //创建收藏夹
    @GetMapping("/create")
    public Result createSCJ(Integer id, String title, String desc, Integer visible) {
        return Result.success(favoriteService.addFavorite(id, title, desc, visible));
    }
    //收藏某视频
    @GetMapping("/add")
    public Result addSCJ(Integer id, Integer vid, Integer fids) {
        favoriteVideoService.addToFav(id,vid,fids);
        return Result.success();
    }
    //取消收藏某视频
    @GetMapping("/del")
    public Result delSCJ(Integer id,Integer vid, Integer fids) {
        favoriteVideoService.removeFromFav(id,vid,fids);
        return Result.success();
    }
    //更新收藏夹消息
    @PostMapping("/update")
    public Result updateSCJ(@RequestBody Favorite favorite) {
        return Result.success(favoriteService.updateFavorite(favorite));
    }

    //删除收藏夹
    @PostMapping("/delSCJ")
    public Result delSCJ(@RequestBody Favorite favorite) {
        return Result.success(favoriteService.delFavorite(favorite.getFid(),favorite.getUid()));
    }


    //最近点赞框
    @GetMapping("/findAllDZ")
    public Result findAllDY(Integer id) {
        List<Integer> vids = userVideoService.getList(id);
        List<Video> videos =  new ArrayList<>();
        for (Integer vid : vids) {
            videos.add(videoService.getVideoWithDataById(vid));
        }
        return Result.success(videos);
    }

    //取消点赞
    @GetMapping("/delDZ")
    public Result delDZ( Integer vid) {
        Integer id = 1;
        userVideoService.setLoveOrUnlove(id,vid,false,true);
        return Result.success();
    }


    //动态框
    //获取所有动态列表
    @GetMapping("/findAllDT")
    public List<Dynamic> findAllDT(Integer did) {
        return dynamicService.getVideosByIds(did);
    }
    //创建动态
    @GetMapping("/createDT")
    public Result createDT(Dynamic dynamic) {
        try {
            return Result.success(dynamicService.addDynamic(dynamic));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //删除动态
    @GetMapping("/delDT")
    public Result delDT(Integer did) {
        try {
            return Result.success(dynamicService.delDynamic(did));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //更新动态
    @GetMapping("/updateDT")
    public Result updateDT(Dynamic dynamic) {
        try {
            return Result.success(dynamicService.updateDynamic(dynamic));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
